﻿using System;
using System.Data;
using System.Linq;
using System.Text;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BO.News;
using BO.Photo;
using GUI.Extensions;

namespace GUI.Controls.Photo
{
    public partial class BoxPhotoEmbed : System.Web.UI.UserControl
    {
        private const string RelateNewsFormat = "<li><a href=\"{0}\" title=\"{2}\"><span class=\"liststyle3 sprite fl\"></span>{1}</a></li>";

        private const string BullFormat = "<a class=\"photo-embed-sprite photo-embed-bull\" href=\"javascript:void(0)\"></a>";

        private const string SlideFormat = "<a class=\"mcgSlideItem\" href=\"{0}\" title=\"{1}\">{2}</a>";

        private const string LinkFormat = "<a  href=\"{0}\" title=\"{2}\">{1}</a>";

        private const int ImageWidth = 300;
        private const int ImageHeight = 225;
        private const int Top = 4;
        private const int TopVisibleSlide = 3;

        protected void Page_Load(object sender, EventArgs e)
        {
            var objCat = RequestExtensions.Category;

            if (objCat != null)
            {

                var enableHtmlCache = CacheSettingExtensions.EnableCacheHtml;
                var bo = enableHtmlCache ? NewsBO_NoCache.GetInstance() : NewsBO.GetInstance();
                var dt = bo.GetNewsByType(Top, DefinedConstants.News.Types.PhotoNews, objCat.ParentId > 0 ? objCat.ParentId : objCat.Id);

                Visible = DataBind(dt);

                if (Visible)
                {
                    var r = dt.Rows[0];
                    var newsId = PresentationUtils.Object2Long(r[SqlConstants.NewsPublish.Id].ToString());
                    if (newsId > 0)
                    {
                        var photoBo = enableHtmlCache ? PhotoBO_NoCache.GetInstance() : PhotoBO.GetInstance();
                        dt = photoBo.GetPhotosInNews(newsId, top: TopVisibleSlide);
                        if (dt != null && dt.Rows.Count > 0)
                        {
                            var sb = new StringBuilder();
                            foreach (var avatar in from DataRow row in dt.Rows
                                                   select PresentationUtils.Photo.BuildPhoto(
                                                       row[SqlConstants.PhotoPublished.ImageUrl].ToString(),
                                                       r[SqlConstants.NewsPublish.Title].ToString(),
                                                       ImageWidth, ImageHeight))
                            {
                                ltrBull.Text += BullFormat;
                                sb.AppendFormat(SlideFormat,
                                                r[SqlConstants.NewsPublish.Url],
                                                r[SqlConstants.NewsPublish.EncodedTitle],
                                                avatar);
                            }
                            ltrGallery.Text = sb.ToString();
                            ltrTitle.Text = string.Format(LinkFormat, r[SqlConstants.NewsPublish.Url],
                                                          r[SqlConstants.NewsPublish.Title],
                                                          r[SqlConstants.NewsPublish.EncodedTitle]);

                            ltrPublishedDate.Text =
                                PresentationUtils.Object2DateTime(r[SqlConstants.NewsPublish.DistributionDate].ToString()).
                                    ToString("dd/MM/yyyy HH:mm");
                        }
                    }
                }
                return;
            }
            Visible = false;
        }

        private bool DataBind(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                var sb = new StringBuilder();
                for (var i = 1; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    sb.AppendFormat(RelateNewsFormat,
                                    r[SqlConstants.NewsPublish.Url],
                                    r[SqlConstants.NewsPublish.Title],
                                    r[SqlConstants.NewsPublish.EncodedTitle]);
                }
                ltrRelateNews.Text = sb.ToString();
                return true;
            }
            return false;
        }
    }
}